package org.xxxy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.xxxy.mapper.CredentialsMapper;
import org.xxxy.pojo.Credentials;

import java.util.Collections;
import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private CredentialsMapper credentialsMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Credentials> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Credentials credentials = credentialsMapper.selectOne(queryWrapper);

        if (credentials == null) {
            String errorMsg = "user" + username + "not found";
            System.out.println(errorMsg);
            throw new UsernameNotFoundException(errorMsg);
        }
        List<GrantedAuthority>authorityList=Collections.singletonList((GrantedAuthority) ()-> "USER");
        return new User(credentials.getName(), credentials.getPassword(), authorityList);

    }
}
